package vendas.web.action;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import vendas.business.LoginBusiness;
import vendas.dto.User;
import gminet.infra.business.BusinessException;
import gminet.infra.db.TransactionException;
import gminet.infra.web.WebException;

@ManagedBean(name = "loginAction")
@RequestScoped
public class LoginAction extends WebAction {

    private static final long serialVersionUID = 2793410025683297380L;
    
    private User user;

    public LoginAction() {
    }

    public String prepareLogin() {

        // TODO RECUPERA DADOS DE ACESSO DO ARQUIVO DE PROPRIEDADES 
        getUser().setEmail(getAccessEmail());
        getUser().setPassword(getAccessPassword());
        //
        return "loginUser";
    }

    public String executeLogin() throws WebException {

        String pageReturn = "";
        try {
            User locaUser = LoginBusiness.executeLogin(getUser());
            setLocalUser(locaUser);
            pageReturn = "homeUser";
        } catch (BusinessException be) {
            addBusinessException("frmLoginUser", be);
            pageReturn = "loginUser";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String executeLogout() {

        releaseLocalUser();
        releaseSession();
        return "index";
    }

    public User getUser() {
        if (this.user == null) {
            this.user = new User();
        }
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
